package com.tzbank.wisdom.service.impl;

import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.constant.SysConstans;
import com.tzbank.wisdom.service.CreditCardService;
import com.tzbank.wisdom.utils.ESBFileDownloadUtil;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * zhangzehuan
 */
@Slf4j
@Service(value = "creditCardService")
public class CreditCardServiceImpl implements CreditCardService {
    @Autowired
    private Environment environment;

    /**
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> getCreditCardLmt(Map<String, String> map) {

        String chkPwdFlag = map.get("chkPwdFlag");
        log.info("getCreditCardLmt请求入参{}:" + map);
        String creditCardNo = map.get("creditCardNo");
        String[] split = creditCardNo.split(",");
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_LMT.getServiceCode(),
                TransCodeEnum.CREDIT_LMT.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//

        inputMap.put("chkPwdFlag", chkPwdFlag);//检查密码标志
        inputMap.put("tranPassword", "");//交易密码
        Map<String, Object> result = new HashMap<>();
        List<Map<String, String>> resultList = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            inputMap.put("creditCardNo", split[i]);//信用卡号
            result = EsbConsumerUtil.clientRequest(inputMap);
            if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
                return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                        result.get("data"));
            } else {
                Map<String, String> resultMap = new HashMap<>();
                Map<String, String> dataMap = (Map<String, String>) result.get("data");
                resultMap.put("creditCardNo", dataMap.get("creditCardNo"));
                resultMap.put("creditLmt", dataMap.get("creditLmt"));
                resultMap.put("curAvlLmt", dataMap.get("curAvlLmt"));
                resultMap.put("cashCreditLmt", dataMap.get("cashCreditLmt"));
                resultMap.put("cashCurAvlLmt", dataMap.get("cashCurAvlLmt"));
                resultList.add(resultMap);
            }
        }
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("dataList", resultList);
        result.put("data", dataMap);
        log.info("getCreditCardLmt响应数据{}", JSONObject.fromObject(result));
        return ResultUtil.resultMap("额度查询", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> getCreditCardInfo(Map<String, String> map) {
        String chkPwdFlag = map.get("chkPwdFlag");
        log.info("getCreditCardInfo请求入参{}:" + map);
        String creditCardNo = map.get("creditCardNo");
        String[] split = creditCardNo.split(",");
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_LMT.getServiceCode(),
                TransCodeEnum.CREDIT_LMT.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//
        // inputMap.put("busName", busName);//业务名称

        inputMap.put("chkPwdFlag", chkPwdFlag);//检查密码标志
        inputMap.put("tranPassword", "");//交易密码
        Map<String, Object> result = new HashMap<>();
        List<Map<String, String>> resultList = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            inputMap.put("creditCardNo", split[i]);//信用卡号
            result = EsbConsumerUtil.clientRequest(inputMap);
            if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
                return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                        result.get("data"));
            } else {
                Map<String, String> dataMap = (Map<String, String>) result.get("data");
                Map<String, String> resultDataMap = new HashMap<>();
                resultDataMap.put("creditCardNo", dataMap.get("creditCardNo"));//信用卡号
                resultDataMap.put("curRepayAmt", dataMap.get("curRepayAmt"));//本期还款金额
                resultDataMap.put("repayAmtMin", dataMap.get("repayAmtMin"));//最低还款金额
                resultDataMap.put("balance", dataMap.get("balance"));//账面余额
                resultDataMap.put("billDay", dataMap.get("billDay"));//账单日
                resultDataMap.put("repayAmt", dataMap.get("repayAmt"));//已出账单未还款
                resultDataMap.put("deadline", dataMap.get("deadline"));//截止日期DEADLINE
                resultDataMap.put("creditLmt", dataMap.get("creditLmt"));
                resultDataMap.put("curAvlLmt", dataMap.get("curAvlLmt"));
                resultDataMap.put("cashCreditLmt", dataMap.get("cashCreditLmt"));
                resultDataMap.put("cashCurAvlLmt", dataMap.get("cashCurAvlLmt"));
                result.remove("data");
                resultList.add(resultDataMap);
            }
        }
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("dataList", resultList);
        result.put("data", dataMap);
        log.info("响应【{}】", JSONObject.fromObject(result));

        return ResultUtil.resultMap("我的信用卡", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    /**
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> getInstlPlan(Map<String, String> map) {
        log.info("信用卡分期计划查询入参getInstlPlan{}" + map);
        String tranType = map.get("tranType");
        String instlType = map.get("instlType");
        String ccy = map.get("ccy");
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_INSTL_PLAN.getServiceCode(),
                TransCodeEnum.CREDIT_INSTL_PLAN.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//
        inputMap.put("tranType", tranType);//交易类型    0－查询分期计划 1－查询分期期数
        inputMap.put("instlType", instlType);//分期类型 A：灵活分期 B：现金分期 M：邮购分期 P：POS分期 Y：账单分期
        inputMap.put("ccy", ccy);//币种
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        log.info("贷记卡分期付款信息查询响应{}" + JSONObject.fromObject(result));
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        } else {
            Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
            List<Map<String, String>> listArray = (List<Map<String, String>>) dataMap.get("listArray");
            List<String> noList = new ArrayList<>();
            List<String> nameList = new ArrayList<>();
            for (int i = 0; i < listArray.size(); i++) {
                Map<String, String> map1 = listArray.get(i);
                if (!noList.contains(map1.get("instlPlayNo"))) {
                    noList.add(map1.get("instlPlayNo"));
                    nameList.add(map1.get("instlPlayName"));
                }
            }
            List<Map<String, Object>> list = new ArrayList<>();
            for (int i = 0; i < nameList.size(); i++) {
                Map<String, Object> map2 = new HashMap<>();
                List<String> periodList = new ArrayList<>();
                for (int j = 0; j < listArray.size(); j++) {
                    Map<String, String> map1 = listArray.get(j);
                    if (nameList.get(i).equals(map1.get("instlPlayName"))) {
                        periodList.add(map1.get("period"));
                    }
                }
                map2.put("instlPlayNo", noList.get(i));
                map2.put("period", periodList);
                map2.put("name", nameList.get(i));
                list.add(map2);
            }
            dataMap.remove("listArray");
            dataMap.remove("head");
            dataMap.put("namePeriodList", list);
            //  dataMap.put("instlPlayNoList", noList);
            result.put("data", dataMap);
        }
        return ResultUtil.resultMap("分期计划查询成功", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> instlTrial(Map<String, String> map) {
        log.info("信用卡分期试算入参instlTrial{}:" + JSONObject.fromObject(map));
        String instlPlayNo = map.get("instlPlayNo");
        String instlType = map.get("instlType");
        String ccy = map.get("ccy");
        String creditCardNo = map.get("creditCardNo");
        String chkPwdFlag = map.get("chkPwdFlag");
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_INSTLTRIA.getServiceCode(),
                TransCodeEnum.CREDIT_INSTLTRIA.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//reqSysHead
        inputMap.put("creditCardNo", creditCardNo);//6223212000288109贷记卡卡号
        inputMap.put("instlType", instlType);//分期类型  A：灵活分期 B：现金分 M：邮购分期 P：POS分期 Y：账单分期 Y
        inputMap.put("instlPlayNo", instlPlayNo);//分期计划编号0006
        inputMap.put("chkPwdFlag", chkPwdFlag);//检查密码标志0
        inputMap.put("ccy", ccy);//币种\0
        inputMap.put("instlAmt", map.get("instlAmt"));//分期金额
        inputMap.put("period", map.get("period"));//分期期数0
        inputMap.put("productCode", map.get("productCode"));//分期商品编号
        inputMap.put("password", map.get("password"));//密码
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);

        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        } else {
            Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
            System.out.println(dataMap);
            DecimalFormat df = new DecimalFormat("#.000000");
            String period = (String) dataMap.get("period");
            Double instlFeeAmt = (Double) dataMap.get("instlFeeAmt");
            Double installmentRepayAmt = (Double) dataMap.get("installmentRepayAmt");
            double totalAmt = 0.0;
            double everyFeeAmt = 0.0;
            everyFeeAmt = Double.valueOf(instlFeeAmt) / Double.valueOf(period);
            System.out.println(df.format(everyFeeAmt));
            totalAmt = everyFeeAmt + Double.valueOf(installmentRepayAmt);
            dataMap.put("everyFeeAmt", df.format(everyFeeAmt));
            dataMap.put("totalAmt", df.format(totalAmt));
            dataMap.remove("head");
            result.put("data", dataMap);
        }
        log.info("信用卡分期试算响应instlTrial{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("分期试算成功", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> getInstlBill(Map<String, String> map) {
        log.info("信用卡可灵活分期分期账单入参getInstlBill{}:" + JSONObject.fromObject(map));
        String instlPlayNo = map.get("instlPlayNo");
        String queryManner = map.get("queryManner");
        String queryType = map.get("queryType");
        String creditCardNo = map.get("creditCardNo");
        String chkPwdFlag = map.get("chkPwdFlag");
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_INSTLBILL.getServiceCode(),
                TransCodeEnum.CREDIT_INSTLBILL.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//
        // inputMap.put("busName", busName);//业务名称
        inputMap.put("creditCardNo", creditCardNo);//6223217000006112
        inputMap.put("chkPwdFlag", chkPwdFlag);//0验密标志
        inputMap.put("queryType", queryType);//1查询类型
        inputMap.put("queryManner", queryManner);//1查询方式
        inputMap.put("instlPlayNo", instlPlayNo);//0006信用计划编号
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        } else {
            List bilList = new ArrayList();
            String[] indexs = {"monthNo", "transferDate", "recordDate", "consumeTime", "seriaNo", "tranType", "tranAmt", "tranFlag", "transferNum",
                    "desc1", "desc2", "merchType", "cardLastFourNo", "ccy", "instlFlag", "date", "flag", "retain"};
            Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
            Map<String, Object> headMap = (Map<String, Object>) dataMap.get("head");
            String filePath = (String) headMap.get("filePath");
            ESBFileDownloadUtil.downloadFile(filePath, "/home/admin/tzbank/esbfile/instlBill/" + filePath, environment);
            try

            {
                bilList = ESBFileDownloadUtil.readFile("/home/admin/tzbank/esbfile/instlBill/" + filePath, indexs);
            } catch (
                    IOException e)

            {
                e.printStackTrace();
            }

            dataMap.remove("head");
            dataMap.put("billList", bilList);
            result.put("data", dataMap);
        }
        log.info("信用卡可灵活分期分期账单响应getInstlBill{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("贷记卡可灵活分期付款的交易明细查询", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> creditRepay(Map<String, String> map) {
        log.info("信用卡还款入参creditRepay{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead1 = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_REPAY.getServiceCode(), TransCodeEnum.CREDIT_REPAY.getServiceSence(), "0110", "0110", "");
        reqSysHead1.put("eClientNo", "500000604");
        inputMap.put("sysHead", reqSysHead1);//
        inputMap.put("selfFlag", map.get("selfFlag"));//1是否本人标志
        inputMap.put("tranPassword", map.get("tranPassword"));//1交易密码
        inputMap.put("creditCardNo", map.get("creditCardNo"));//贷记卡卡号6223218000024105
        inputMap.put("payerAcctNo", map.get("payerAcctNo"));//付款人账号6224271190391118
        inputMap.put("payerName", map.get("payerName"));//付款人名称缘尽
        inputMap.put("repayAmt", map.get("repayAmt"));//还款金额2000
        inputMap.put("globalType", map.get("globalType"));//证件类型P00
        inputMap.put("globalId", map.get("globalId"));//证件号码370404199001125748
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
        dataMap.remove("head");
        result.put("data", dataMap);
        log.info("信用卡还款响应creditRepay{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("贷记卡还款", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> saveBillInstl(Map<String, String> map) {
        log.info("信用卡账单分期提交入参creditRepay{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead1 = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_SAVEBILLINSTL.getServiceCode(), TransCodeEnum.CREDIT_SAVEBILLINSTL.getServiceSence(), "", "0110", "");
        inputMap.put("sysHead", reqSysHead1);//
        inputMap.put("tranPassword", map.get("tranPassword"));//1
        //inputMap.put("tokenPwd", "1");//1
        inputMap.put("creditCardNo", map.get("creditCardNo"));//贷记卡卡号6223217000006112
        inputMap.put("operationType", map.get("operationType"));//操作类型1
        inputMap.put("instlType", map.get("instlType"));//分期类型Y
        inputMap.put("ccy", map.get("ccy"));//币种156
        inputMap.put("creditPlayNo", map.get("creditPlayNo"));//信用计划编号0006
        inputMap.put("instlAmt", map.get("instlAmt"));//分期金额2000
        inputMap.put("period", map.get("period"));//期数6
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);

        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
        dataMap.remove("head");
        result.put("data", dataMap);
        log.info("信用卡账单分期提交响应creditRepay{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("账单分期申请提交 ", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> saveFiexBillInstl(Map<String, String> map) {
        log.info("灵活分期申请提交入参saveFiexBillInstl{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead1 = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_SAVEFIEXBILLINSTL.getServiceCode(), TransCodeEnum.CREDIT_SAVEFIEXBILLINSTL.getServiceSence(), "", "0110", "");
        inputMap.put("sysHead", reqSysHead1);
        inputMap.put("creditCardNo", map.get("creditCardNo"));//贷记卡卡号6223217000006112
        inputMap.put("queryManner", map.get("queryManner"));//查询方式1
        inputMap.put("instlType", map.get("instlType"));//付款人名称A
        // inputMap.put("ccy", "156");//还款金额
        inputMap.put("creditPlayNo", map.get("creditPlayNo"));//证件类型0006
        inputMap.put("instlAmt", map.get("instlAmt"));//分期金额2000
        inputMap.put("period", map.get("period"));//期数6
        inputMap.put("tranType", map.get("tranType"));//交易类型1
        // inputMap.put("tokenPwd", "1");//1
        inputMap.put("registerDate", map.get("registerDate"));//20120202登记日期
        inputMap.put("inAcctDate", map.get("inAcctDate"));//0202入账日期
        inputMap.put("consumerTime", map.get("consumerTime"));//123消费时间
        inputMap.put("seqNo", map.get("seqNo"));//流水号
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);

        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
        dataMap.remove("head");
        result.put("data", dataMap);
        log.info("灵活分期申请提交响应saveFiexBillInstl{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("灵活分期申请提交 ", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> getBill(Map<String, String> map) {
        log.info("贷记卡未出帐单明细查询入参getBill{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_GETBILL.getServiceCode(),
                TransCodeEnum.CREDIT_GETBILL.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//
        //inputMap.put("busName", "CreditInstalFeeCalc");//业务名称
        inputMap.put("creditCardNo", map.get("creditCardNo"));
        inputMap.put("options", map.get("options"));//保留字段L:　　本币 默认 I:　　外币 A:    所有币种
        inputMap.put("appBranchNo", map.get("appBranchNo"));
        inputMap.put("appUserId", map.get("appUserId"));
        inputMap.put("chkPwdFlag", map.get("chkPwdFlag"));//检查密码标志
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        } else {
            List bilList = new ArrayList();
            String[] indexs = {"monthNo", "transferDate", "recordDate", "consumeTime", "seriaNo", "tranType", "tranAmt", "tranFlag", "transferNum",
                    "desc1", "desc2", "merchType", "cardLastFourNo", "ccy", "instlFlag", "date", "flag", "retain"};
            Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
            Map<String, Object> headMap = (Map<String, Object>) dataMap.get("head");
            String filePath = (String) headMap.get("filePath");
            ESBFileDownloadUtil.downloadFile(filePath, "/home/admin/tzbank/esbfile/bill/" + filePath, environment);
            try

            {
                bilList = ESBFileDownloadUtil.readFile("/home/admin/tzbank/esbfile/bill/" + filePath, indexs);
            } catch (
                    IOException e)

            {
                e.printStackTrace();
            }

            dataMap.remove("head");
            dataMap.put("billList", bilList);
            result.put("data", dataMap);
        }
        log.info("贷记卡未出帐单明细查询响应getBill{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("贷记卡未出帐单明细查询", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> getOutBill(Map<String, String> map) {
        log.info("贷记卡已出帐单明细查询入参getOutBill{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_GETOUTBILL.getServiceCode(),
                TransCodeEnum.CREDIT_GETOUTBILL.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//
        inputMap.put("creditCardNo", map.get("creditCardNo"));//贷记卡卡号6223217000006112
        inputMap.put("billDay", map.get("billDay"));//账单月份1806
        inputMap.put("options", map.get("options"));//保留
        inputMap.put("appBranchNo", map.get("appBranchNo"));//经办机构
        inputMap.put("appUserId", map.get("appUserId"));//经办柜员
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);

        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));

        } else {
            List bilOutList = new ArrayList();
            String[] indexs = {"monthNo", "transferDate", "recordDate", "consumeTime", "seriaNo", "tranType", "tranAmt", "tranFlag", "transferNum",
                    "desc1", "desc2", "merchType", "cardLastFourNo", "ccy", "instlFlag", "date", "flag", "retain"};
            Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
            Map<String, Object> headMap = (Map<String, Object>) dataMap.get("head");
            String filePath = (String) headMap.get("filePath");
            ESBFileDownloadUtil.downloadFile(filePath, "/home/admin/tzbank/esbfile/outbill/" + filePath, environment);
            try

            {
                bilOutList = ESBFileDownloadUtil.readFile("/home/admin/tzbank/esbfile/outbill/" + filePath, indexs);
            } catch (
                    IOException e)

            {
                e.printStackTrace();
            }

            dataMap.remove("head");
            dataMap.put("bilOutList", bilOutList);
            result.put("data", dataMap);
        }
        log.info("贷记卡已出帐单明细查询响应getOutBill{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("贷记卡已出帐单明细查询", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> getOutBillTotal(Map<String, String> map) {
        log.info("贷记卡已出帐单total查询入参getOutBillTotal{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_GETOUTBILL_TOTAL.getServiceCode(),
                TransCodeEnum.CREDIT_GETOUTBILL_TOTAL.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//
        inputMap.put("creditCardNo", map.get("creditCardNo"));//贷记卡卡号6223217000006112
        inputMap.put("billYears", map.get("billYears"));//交易类型
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
        dataMap.remove("head");
        result.put("data", dataMap);
        log.info("贷记卡已出帐单total查询响应getOutBillTotal{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("贷记卡已出帐单total查询", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> instlOutBillInfo(Map<String, String> map) {
        log.info("贷记卡已出帐单分期信息查询入参getOutBillTotal{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_INSTLOUTBILL_INFO.getServiceCode(),
                TransCodeEnum.CREDIT_INSTLOUTBILL_INFO.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//
        inputMap.put("creditCardNo", map.get("creditCardNo"));//贷记卡卡号6223217000006112
        inputMap.put("tranType", map.get("tranType"));//交易类型
        inputMap.put("ccy", map.get("ccy"));
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
        dataMap.remove("head");
        result.put("data", dataMap);
        log.info("贷记卡已出帐单分期信息查询响应getOutBillTotal{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("贷记卡已出帐单分期信息查询成功", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> getAutoRepay(Map<String, String> map) {
        log.info("信用卡自动签约关系查询入参getAutoRepay{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_GETAUTOREPAY.getServiceCode(),
                TransCodeEnum.CREDIT_GETAUTOREPAY.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//]
        inputMap.put("busName", "");
        inputMap.put("creditCardNo", map.get("creditCardNo"));//贷记卡卡号6223218000024105
        inputMap.put("chkPwdFlag", map.get("chkPwdFlag"));//0
        inputMap.put("flag", "1");//
        inputMap.put("ccy", map.get("ccy"));//156

        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        boolean bool = true;
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            Map<String, Object> dataMap = new HashMap<>();
            bool = false;
            dataMap.put("flag", bool);
            result.put("code", "200");
            result.put("data", dataMap);
            return result;
        }
        Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
        dataMap.put("flag", bool);
        dataMap.remove("head");
        result.put("data", dataMap);
        log.info("信用卡自动签约关系查询响应getAutoRepay{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("信用卡自动签约关系查询", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> getInstlBillAmt(Map<String, String> map) {
        log.info("账单可分期余额查询入参getInstlBillAmt{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_INSTL_BILL_AMT.getServiceCode(),
                TransCodeEnum.CREDIT_INSTL_BILL_AMT.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//
        inputMap.put("creditCardNo", map.get("creditCardNo"));//贷记卡卡号6223217000006112
        inputMap.put("tranType", map.get("tranType"));//2交易类型
        inputMap.put("ccy", map.get("ccy"));//币种156
        inputMap.put("creditPlayNo", map.get("creditPlayNo"));//分期计划编号0006
        inputMap.put("instlAmt", map.get("instlAmt"));//分期金额2000
        inputMap.put("period", map.get("period"));//期数6
        inputMap.put("chkPwdFlag", map.get("chkPwdFlag"));//是否验密0
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        log.info("账单可分期余额查询响应getInstlBillAmt{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("账单可分期余额查询 ", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> creditCardOut(Map<String, String> map) {
        log.info("信用卡转出入参creditCardOut{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_CARD_OUT.getServiceCode(),
                TransCodeEnum.CREDIT_CARD_OUT.getServiceSence(),
                "", "0110", "");
        inputMap.put("sysHead", reqSysHead);//
        inputMap.put("payeeAcctNo", map.get("payeeAcctNo"));//收款人账号6224271190391122
        inputMap.put("ccy", map.get("ccy"));//币种156
        inputMap.put("payerAcctNo", map.get("payerAcctNo"));//付款人账号6223217000006112
        inputMap.put("payerAcctType", map.get("payerAcctType"));//付款人账户类型11
        inputMap.put("tranAmt", map.get("tranAmt"));//交易金额6000
        inputMap.put("globalType", map.get("globalType"));//证件类型P00
        inputMap.put("globalId", map.get("globalId"));//证件号码370404199001125748
        inputMap.put("tranType", map.get("tranType"));//交易类型1
        inputMap.put("payeeAcctType", map.get("payeeAcctType"));//收款人账户类型1
        inputMap.put("tranPassword", map.get("tranPassword"));//交易密码20120202
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
        dataMap.remove("head");
        result.put("data", dataMap);
        log.info("信用卡转出响应creditCardOut{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("信用卡转出成功", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public Map<String, Object> sendBill(Map<String, String> map) {
        log.info("信用卡账单寄送入参sendBill{}:" + JSONObject.fromObject(map));
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead1 = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_SENDBILL.getServiceCode(),
                TransCodeEnum.CREDIT_SENDBILL.getServiceSence(),
                "", "0110", "");
        inputMap.put("sysHead", reqSysHead1);//
        inputMap.put("creditCardNo", map.get("creditCardNo"));//
        inputMap.put("acctName", map.get("acctName"));//
        inputMap.put("billSendType", map.get("billSendType"));//EM
        inputMap.put("email", map.get("email"));//

        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);

        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
        dataMap.remove("head");
        result.put("data", dataMap);
        log.info("信用卡账单寄送响应sendBill{}:" + JSONObject.fromObject(result));
        return ResultUtil.resultMap("账单寄送成功", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

    @Override
    public List<String> getCreditCardList(Map<String, String> map) {
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CREDIT_CARD_NO.getServiceCode(),
                TransCodeEnum.CREDIT_CARD_NO.getServiceSence(),
                "0110", "BS",
                SysConstans.ABP_CODE, "");
        inputMap.put("sysHead", reqSysHead);//+
        inputMap.put("globalType", map.get("globalType"));//证件类型
        inputMap.put("globalId", map.get("globalId"));//证件号码450401198405204860
        inputMap.put("appBranchNo", map.get("appBranchNo"));//经办机构
        inputMap.put("appUserId", map.get("appUserId"));//经办柜员
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return null;

        } else {
            List creditCardList = new ArrayList();
            String[] indexs = {"creditCardNo", "effectiveDate", "flag", "startDate", "name", "stauts", "desc1", "desc2", "desc3"};
            Map<String, Object> dataMap = (Map<String, Object>) result.get("data");
            Map<String, Object> headMap = (Map<String, Object>) dataMap.get("head");
            String filePath = (String) headMap.get("filePath");
            ESBFileDownloadUtil.downloadFile(filePath, "/home/admin/tzbank/esbfile/creditCardNo/" + filePath, environment);
            try

            {
                creditCardList = ESBFileDownloadUtil.readFile("/home/admin/tzbank/esbfile/creditCardNo/" + filePath, indexs);
            } catch (
                    IOException e)

            {
                e.printStackTrace();
            }
            List<String> list = new ArrayList<>();
            for (int i = 0; i < creditCardList.size(); i++) {
                Map<String, String> map1 = (Map<String, String>) creditCardList.get(i);
                list.add(map1.get("creditCardNo"));
            }
            return list;
        }

    }
}